package com.example.ktapplication.adapter

import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.ktapplication.R
import com.example.ktapplication.adapter.CommomRvAdapter.OnItemClickListener
import com.example.ktapplication.bean.NavData

class LeftNavigationAdapter(context: Context) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private lateinit var mContext:Context

    var leftList = mutableListOf<NavData>()

    private var mOnItemClickListener: OnItemClickListener? = null

    init {
        mContext=context
    }

    fun setOnItemClickListener(onItemClickListener: OnItemClickListener?) {
        this.mOnItemClickListener = onItemClickListener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val view = LayoutInflater.from(mContext).inflate(R.layout.rv_left_item_layout, parent, false)
        return ContentViewHolder(view)
    }

    override fun getItemCount(): Int {
       return leftList.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val bean: NavData = leftList.get(position)
        val viewHolder = holder as ContentViewHolder
        if (bean.chose){
            val backgroundColor = Color.parseColor("#e0deda")
            viewHolder.llBackgroud.setBackgroundColor(backgroundColor)
            val textColor = Color.parseColor("#3700B3")
            viewHolder.tvLeftTitle.setTextColor(textColor)
        }else{
            val backgroundColor = Color.parseColor("#ffffff")
            viewHolder.llBackgroud.setBackgroundColor(backgroundColor)
            val textColor = Color.parseColor("#000000")
            viewHolder.tvLeftTitle.setTextColor(textColor)
        }
        viewHolder.tvLeftTitle.text=bean.name
        setListener(viewHolder,position)
    }

    private fun setListener(holder: ContentViewHolder, position: Int) {
        holder.itemView.setOnClickListener { view ->
            if (mOnItemClickListener != null) {
                mOnItemClickListener!!.onItemClick(position)
            }
        }
    }

    //内容 ViewHolder
    class ContentViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
         val tvLeftTitle: TextView = itemView.findViewById<TextView>(R.id.tvLeft)
         val llBackgroud: LinearLayout = itemView.findViewById<LinearLayout>(R.id.llBackgroud)
    }

    fun setChoose(position: Int) {
        leftList.forEach {
            it.chose = false
        }
        leftList[position].chose = true
        notifyDataSetChanged()
    }

    fun interface OnItemClickListener {
        fun onItemClick(position: Int)
    }

}

